package com.twu.biblioteca.services;

import com.twu.biblioteca.Constants.Books;
import com.twu.biblioteca.Constants.Messages;
import com.twu.biblioteca.elements.Book;
import com.twu.biblioteca.helper.ServiceHelper;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BookPrintService {
    private BookLogicService bookLogicService;

    public List<Book> showAllAvailableBooks() {
        System.out.println("\nAll available books are:");
        bookLogicService = new BookLogicService(new Books());
        List<Book> availableBooks = bookLogicService.getAllAvailableBooksInTheLibrary();
        for (Book availableBook : availableBooks) {
            System.out.println(availableBook.bookName);
        }
        return availableBooks;
    }

    public String chooseAvailableBook() throws IOException {
        boolean isAvailable = false;
        Map<String, String> chooseBookResult = new HashMap<String, String>();

        while (!isAvailable) {
            chooseBookResult = bookLogicService.getChosenBookNameAndStatus();
            isAvailable = Boolean.parseBoolean(chooseBookResult.get("isAvailable"));
            System.out.println(Messages.ERROR_MESSAGE_WHEN_CHOOSE_UNAVAILABLE_BOOK);
        }
        return chooseBookResult.get("bookName");
    }

    public void checkOutBook(Book book, String owner) {
        if (book == null || !bookLogicService.bookIsAvailable(book.getBookName())) {
            System.out.println(Messages.CHECKOUT_UNAVAILABLE_BOOK);
            return;
        }
        bookLogicService.checkoutBookSuccessfully(book, owner);
    }

    public void returnBook(String returnBookName) {
        boolean returnBookSuccess = bookLogicService.returnBook(returnBookName);
        if (returnBookSuccess) {
            System.out.println(Messages.RETURN_BOOK_SUCCESSFUL);
            return;
        }
        System.out.println(Messages.RETURN_BOOK_FAIL);
    }

    public void inputAvailableBookAndCheckoutSuccess() throws IOException {
        System.out.println("\n" + Messages.INPUT_BOOK_NAME_TO_BE_CHOOSE);
        String chosenBookName = chooseAvailableBook();
        Book chosenBook = bookLogicService.getBookByBookName(chosenBookName);
        System.out.println(Messages.INPUT_READER_NAME);
        String readerName = ServiceHelper.getInputValue();
        bookLogicService.checkoutBookSuccessfully(chosenBook, readerName);
        System.out.println("\n" + Messages.CHECK_OUT_BOOK_SUCCESSFUL);
    }

    public void chooseUnavailableBookAndCheckoutFail() throws IOException {
        System.out.println("\n" + Messages.INPUT_BOOK_NAME_TO_BE_CHOOSE);
        String chosenBookName = bookLogicService.getChosenBookNameAndStatus().get("bookName");
        Book chosenBook = bookLogicService.getBookByBookName(chosenBookName);
        System.out.println(Messages.INPUT_READER_NAME);
        String readerName = ServiceHelper.getInputValue();
        checkOutBook(chosenBook, readerName);
    }

    public void returnBook() throws IOException {
        System.out.println("\n" + Messages.INPUT_RETURN_BOOK_NAME);
        String returnBookName = ServiceHelper.getInputValue();
        returnBook(returnBookName);
    }
}
